//
//  SSEmoticonToolBar.swift
//  weibo
//
//  Created by 邵帅 on 16/7/2.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit

enum toolBarType: Int{
    
    case Recent = 1000
    case Default = 1001
    case Emoji = 1002
    case Lxh = 1003
}
class SSEmoticonToolBar: UIStackView {
    // 点击回调
    var btnClosure: ((btnType: toolBarType)->())?
    
    var currentBtn: UIButton?

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
        axis = .Horizontal
        distribution = .FillEqually
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
        
        addBtn("最近", type: .Recent)
        addBtn("默认", type: .Default)
        addBtn("emoji", type: .Emoji)
        addBtn("浪小花", type: .Lxh)
    }
    
    // 抽取一个创建的方法
    func addBtn(title: String,imageName: String = "compose_emotion_table",type: toolBarType) {
        
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
        btn.setBackgroundImage(UIImage(named:"\(imageName)_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"\(imageName)_selected"), forState: .Selected)
     
        btn.addTarget(self, action: #selector(SSEmoticonToolBar.btnClick(_:)), forControlEvents: .TouchUpInside)
        
        btn.tag = type.rawValue
        addArrangedSubview(btn)
        
        if type == .Default {
            
            btn.selected = true
            currentBtn = btn
        
        }
    }
    
    // 按钮点击
    func btnClick(btn: UIButton) {
        
        currentBtn?.selected = false
        btn.selected = true
        currentBtn = btn
        
        btnClosure?(btnType: toolBarType(rawValue: btn.tag)!)
    }
}
